package com.controller;

import com.api.CommonResult;
import com.entity.BType;
import com.entity.BUser;
import com.entity.table.BGoodsTableDef;
import com.entity.table.BUserTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.BTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.BGoods;
import com.service.BGoodsService;

import java.io.Serializable;
import java.util.List;

/**
 * 商品表 控制层。
 *
 * @author admin
 * @since 2024-05-30
 */
@Controller
@RequestMapping("/admin/good")
public class BGoodsController {

    @Autowired
    private BGoodsService bGoodsService;

    @Autowired
    private BTypeService bTypeService;

    //添加获取商品列表代码
    @GetMapping(value = "/goodList")
    public String goodList(ModelMap model, @RequestParam(value = "goodName", defaultValue = "") String goodName,
                            @RequestParam(value = "p", defaultValue = "1") int p,
                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        try {
            QueryWrapper queryWrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName));
            Page<BGoods> page = new Page<>();
            page.setPageSize(pageSize);//设置每页显示记录数
            page.setPageNumber(p);//设置页码
            Page<BGoods> pageInfo = bGoodsService.getMapper().paginateWithRelations(page,queryWrapper);
            String typename = pageInfo.getRecords().get(0).getBType().getTypeName();
            model.addAttribute("page", pageInfo);
            model.addAttribute("total",pageInfo.getTotalRow());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "back/goods-list";
    }
    @GetMapping(value = "/addView")
    public String addView(Model model) {
        List<BType> typeList = bTypeService.list();
        model.addAttribute("typeList", typeList);
        return "back/goods-add";
    }
    /**
     * 改添加用户表代码。
     *
     * @param bGoods 用户表
     * @return {@code true} 添加成功,{@code false} 添加成员
     */
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(BGoods bGoods) {
        try {
            boolean r = bGoodsService.save(bGoods);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bGoods.getGoodName()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    //添加修改管理员页面代码
    @GetMapping(value = "updateView")
    public String updateView(int id,Model model){
        List<BType> typeList=bTypeService.list();
        BGoods bGood = bGoodsService.getById(id);
        model.addAttribute("typeList", typeList);
        model.addAttribute("bGood", bGood);
        return "back/goods-edit";
    }
    /**
     * 修改根据主键更新用户表代码。
     *
     * @param bGoods 用户表
     * @return {@code true} 更新成功,{@code false} 更新失败
     */
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(BGoods bGoods) {
        try {
            boolean r = bGoodsService.updateById(bGoods);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bGoods.getGoodName()+"已存在";
            return  CommonResult.failed(msg);
        }
    }
    //添加删除用户代码
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id) {
        try {
            boolean r = bGoodsService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
    /**
     * 查询所有商品表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<BGoods> list() {
        return bGoodsService.list();
    }

    /**
     * 根据商品表主键获取详细信息。
     *
     * @param id 商品表主键
     * @return 商品表详情
     */
    @GetMapping("getInfo/{id}")
    public BGoods getInfo(@PathVariable Serializable id) {
        return bGoodsService.getById(id);
    }

    /**
     * 分页查询商品表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<BGoods> page(Page<BGoods> page) {
        return bGoodsService.page(page);
    }

}
